from django.db import models
from tools.random_str import *


# Create your models here.
class Weld(models.Model):
    my_id = models.CharField(max_length=10, default=my_weld_id, primary_key=True, verbose_name='单号')
    pcb = models.ForeignKey('pcb.Pcb', on_delete=models.CASCADE, verbose_name='电路板')
    num = models.IntegerField(default=1, verbose_name='焊接数量')
    author = models.ForeignKey('pcb.PcbAuthor',on_delete=models.CASCADE, verbose_name='焊接人')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=100, blank=True, null=True, verbose_name='描述')

    class Meta:
        verbose_name = '焊接单'
        verbose_name_plural = verbose_name

    def show_author_image(self):
        return self.author.show_image()

    # def pcb_name(self):
    #     return self.pcb.name

    def show_pcb_image(self):
        return self.pcb.show_image()

    show_pcb_image.short_description = '电路板'
    show_author_image.short_description = '焊接人'

    def __str__(self):
        return self.my_id


class WeldList(models.Model):
    weld = models.ForeignKey(Weld, on_delete=models.CASCADE, verbose_name='单号')
    order = models.CharField(max_length=4, verbose_name='序号')
    goods = models.ForeignKey('goods.Goods', on_delete=models.CASCADE, verbose_name='元件')
    num = models.IntegerField(verbose_name='数量')
    desc = models.CharField(max_length=100, blank=True, null=True, verbose_name='描述')

    class Meta:
        verbose_name = '焊接详单'
        verbose_name_plural = verbose_name

    def weld_my_id(self):
        return self.weld.my_id

    def goods_name(self):
        return self.goods.name
    weld_my_id.short_description = '焊接单'
    goods_name.short_description = '元件'

    def __str__(self):
        return self.order
